//
//  XTAppApns.m
//  XTAppExtension
//
//  Created by 谢特 on 2017/5/10.
//  Copyright © 2017年 xiete. All rights reserved.
//

#import "XTAppApns.h"

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

#import <UserNotifications/UserNotifications.h>

#endif

#define SYSTEM_VERSION_iOS8_OR_iOS9 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0f && [[[UIDevice currentDevice] \
systemVersion] floatValue] < 10.0f)

#define  SYSTEM_VERSION_iOS10_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0f)

@implementation XTAppApns
+ (void)registApns{
    if (SYSTEM_VERSION_iOS8_OR_iOS9){
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:
         (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound)
                                          categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    } else if (SYSTEM_VERSION_iOS10_LATER){
        UNUserNotificationCenter *notiCenter = [UNUserNotificationCenter currentNotificationCenter];
        if (![[[UIApplication sharedApplication] delegate] conformsToProtocol:@protocol(UNUserNotificationCenterDelegate)]){
            NSLog(@"AppDelegate需要遵守UNUserNotificationCenterDelegate协议才能监听通知");
        }
        notiCenter.delegate = (id <UNUserNotificationCenterDelegate> ) [[UIApplication sharedApplication] delegate];
        [notiCenter requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (granted){ //允许授权远程推送
                
                NSLog(@"注册成功");
                [notiCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
                    NSLog(@"%@",settings);
                }];
                
            }else{ //授权失败
                NSLog(@"注册失败");
            }
        }];
    }else{
         [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
    }
     [[UIApplication sharedApplication] registerForRemoteNotifications];
}
@end
